package com.cloudpivot.receivales.meta.service.impl;

import com.cloudpivot.receivales.meta.bsweaverdocws.DocAttachment;
import com.cloudpivot.receivales.meta.bsweaverdocws.DocInfo;
import com.cloudpivot.receivales.meta.bsweaverdocws.DocServiceLocator;
import com.cloudpivot.receivales.meta.bsweaverdocws.DocServicePortType;
import com.cloudpivot.receivales.meta.service.QCFileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

@Service
public class QCFileServiceImpl implements QCFileService {

    @Value("${oadocurl.path}")
    private String OAPATH;
    @Value("${oadocurl.loginname}")
    private String LOGINUSER;
    @Value("${oadocurl.loginpassword}")
    private String LOGINPWS;
    @Value("${oadocurl.loginip}")

    private String LOGINIP;

    /**
     * 上传 oa 质量文档 传入文档全部为 base64
     * @param sourceFile     源文件base64
     * @param pdfFile    编译后的文件base64
     * @param sourceFileType 源文件 类型 "docx" or "xlsx"
     * @param filename      文件名
     * @param titleName     档案名(OA知识文件显示的名称)
     * @param fileDic     文档存放的目录
     */
    @Override
    public Integer qcFileUpload(String sourceFile,String pdfFile,String sourceFileType, String filename, String titleName,Integer fileDic) {
        try {
            DocServicePortType servicePortType = new DocServiceLocator().getDocServiceHttpPort(new URL(OAPATH));
            String tokenSession = servicePortType.login(LOGINUSER, LOGINPWS, 0, LOGINIP);
            DocAttachment sourceDa = new DocAttachment();
            sourceDa.setDocid(0);
            sourceDa.setImagefileid(0);
            sourceDa.setFilecontent(sourceFile);
            sourceDa.setIszip(0);
            sourceDa.setFilename(filename+"."+sourceFileType);//文档名称原件
            sourceDa.setIsextfile("1");
            sourceDa.setDocfiletype("3");
            DocAttachment pdfDa = new DocAttachment();
            pdfDa.setDocid(0);
            pdfDa.setImagefileid(0);
            pdfDa.setFilecontent(pdfFile);
            pdfDa.setIszip(0);
            pdfDa.setFilename(filename+".pdf");//文档名称原件
            pdfDa.setIsextfile("1");
            pdfDa.setDocfiletype("3");

            DocInfo wsdoc = new DocInfo();//创建文档
            wsdoc.setDoccreaterid(928);//创建人ID
            wsdoc.setDoccreatertype(0);
            wsdoc.setAccessorycount(1);
            wsdoc.setMaincategory(1);//主目录id
            wsdoc.setSubcategory(1);//分目录id
            wsdoc.setSeccategory(fileDic);//子目录id
            wsdoc.setOwnerid(928);
            wsdoc.setDocStatus(1);//文档状态
            wsdoc.setId(0);
            wsdoc.setDocType(2);
            wsdoc.setDocSubject(titleName);//知识中文档的名称,一般与上面的文档名称一致即可
//            wsdoc.setDoccontent(titleName);//知识中文档的名称,一般与上面的文档名称一致即可
            wsdoc.setAttachments(new DocAttachment[] { pdfDa, sourceDa}); //放入文档组
            Integer resultDocId=servicePortType.createDoc(wsdoc, tokenSession);
            return resultDocId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public Integer qcFileUploadSigle(String baseFile64, String sourceFileType, String filename, String titleName,Integer fileDic) {
        try {
            DocServicePortType servicePortType = new DocServiceLocator().getDocServiceHttpPort(new URL(OAPATH));
            String tokenSession = servicePortType.login(LOGINUSER, LOGINPWS, 0, LOGINIP);

            DocAttachment da = new DocAttachment();
            da.setDocid(0);
            da.setImagefileid(0);
            da.setFilecontent(baseFile64);
            da.setIszip(0);
            da.setFilename(filename);//文档名称+后缀
            da.setIsextfile("1");
            da.setDocfiletype("3");

            DocInfo wsdoc = new DocInfo();//创建文档
            wsdoc.setDoccreaterid(928);//创建人ID
            wsdoc.setDoccreatertype(0);
            wsdoc.setAccessorycount(1);
            wsdoc.setMaincategory(1);//主目录id
            wsdoc.setSubcategory(1);//分目录id
            wsdoc.setSeccategory(fileDic);//子目录id
            wsdoc.setOwnerid(928);
            wsdoc.setDocStatus(1);//文档状态
            wsdoc.setId(0);
            wsdoc.setDocType(2);
            wsdoc.setDocSubject(titleName);//知识中文档的名称,一般与上面的文档名称一致即可
//            wsdoc.setDoccontent(titleName);//文档中html的内容
            wsdoc.setAttachments(new DocAttachment[] { da}); //放入文档组
            Integer resultDocId=servicePortType.createDoc(wsdoc, tokenSession);
            return resultDocId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
